// pages/addinfo/addinfo.js
const request=require('../../util/request.js')
const instruct = require('../../util/tspl')
import bleDisNetwork from '../../util/bleDisNetwork'
Page({

  /**
   * 页面的初始数据
   */
  data: {
   
    list:[],
    page: 1,//页码
    limit: 5,//每页数量
    type:0,//可不传参数
    keyword:"",//可不传参数
    add_time:"",//可不传参数
    p_no:"",//项目号
    count: 0,
    flag: true,
    projectbody:[],//项目主体信息
    datastatus:2,//查看 2全部/1以完善/0未完成 
    datafind:"",//搜索关键字
    date:"",
    mindate:Math.round((new Date() - 365*86400*1000)),
    findtime:"",//时间范围查找
    __returnParams:false,
    showSelList:false,
    value1:'',
    value2:'',
    value3:'',
    value4:'',
    value5:'',
    value6:'',
    worker: "",//工人
    card: "",//卡号
    note: "",//摘要
    code: "",//条码
    cust_no: "",//供应商或客商号
    activeNames: [], // 当前展开的面板
    product_name: ""//货品
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      __returnParams:false,

    })
    this.data.p_no = options.p_no
    this.data.page = 1
    this.getList()

  },
  
  getList(search="",date="") {
    var that = this;
    var params = {
      page: that.data.page,//当前页码1
      limit: that.data.limit,
      add_time:this.data.findtime,
      status:this.data.datastatus,
      worker: this.data.worker,//工人
      card:this.data.card,
      note:this.data.note,
      code:this.data.code,
      cust_no:this.data.cust_no,
      product_name:this.data.product_name,
    }
    console.log(params,"---------------")
    request.requestAjax('fruit/get_report_works', params, "加载中",function (res) {
        console.log(res,"ressssssssssss")
        if (res.status === 200) {
          let list = that.data.list
          res.data.data.map((item) =>{
            return item.t = false;
          })
          if (that.data.page == 1) {
            list = []
          }
          list = [...list, ...res.data.data]
          if (list.length < res.data.count ){
            that.setData({
              page: that.data.page + 1,
              flag:true
            })
          } else {
            that.setData({
              flag:false
            })
          }
          that.setData({
            list: list,
            count: res.data.count
          })
        }
      }, function (res) {
        console.log(res,"请求失败")
      },
    )

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },
  submit(event){
  },
  bindKeyInputVant(event){
    let valueName = event.target.dataset.variable;
    let name = `formData.${valueName}`;
    this.setData({
      [valueName]:event.detail
    });
  },
  onTabChange(event){
    if(event.detail.index === 1){
      this.data.datastatus = 1
    }else if(event.detail.index === 2){
      this.data.datastatus = 0
    }else if(event.detail.index === 0){
      this.data.datastatus = 2
    }
    this.data.page = 1
    this.getList()
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
  },
  onDisplay() {
    this.setData({ show: true });
  },
  onClose() {
    this.setData({ show: false });
  },
  formatDate(date) {
    date = new Date(date);
    return `${date.getFullYear()}-${date.getMonth()+1}-${date.getDate()}`;
  },
  onConfirm(event) {
    this.data.page = 1
    this.setData({
      show: false,
      date: this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1]),
      findtime : this.formatDate(event.detail[0])+' - '+this.formatDate(event.detail[1])
    });
    this.getList("",this.data.date)
  },

  onSearch(e){
    this.setData({
      flag:true
    })
    this.data.page = 1
    this.data.datafind = e.detail
    wx.pageScrollTo({
      scrollTop: 0,
      duration: 0
    })
    this.getList(e.detail)
  },
  onReachBottom() {
    console.log('加载')
    if(this.data.flag){
      this.setData({
        flag:false
      })
      this.getList(); // 疯狂的请求的方法
    }
  },
  comment(event){
    let tmp = event.currentTarget.dataset.index
    let pic_no = ""
    for(let i = 0;i<this.data.projectbody.length;i++){
      if(this.data.projectbody[i].id === tmp){
        pic_no = this.data.projectbody[i].pic_no
      }
    }
    wx.navigateTo({
      url: `/pages/pic_no_list_process/pic_no_list_process?p_no=${tmp}&pic_no=${pic_no}`,
    })
  },

  setrem(e){
    wx.navigateTo({
      url: `/pages/pic_no_rember/pic_no_rember?FInterID=${e.currentTarget.dataset.index}&fbillno=${e.currentTarget.dataset.fbillno}`,
    })
  },

  set_bao_gong(){
    wx.navigateTo({ url: '/pages/set_bao_gong/set_bao_gong' })
  },

  showSel(e){
    var that = this
    this.setData({
      showSelList:!that.data.showSelList
    })
  },

  //工人搜索
  onSearch_worker(e){
    var that = this
    this.setData({
      showSelList:!that.data.showSelList,
      page:1,
      worker:e.detail,
      value1:e.detail,
    })
    this.getList()
  },
  //卡号搜索
  onSearch_card(e){
    var that = this
    this.setData({
      showSelList:!that.data.showSelList,
      page:1,

      card:e.detail,
      value2:e.detail,
    })
    console.log(this.data.card,"ddddddddd")
    this.getList()
  },
  //摘要搜索
  onSearch_note(e){
    var that = this
    this.setData({
      showSelList:!that.data.showSelList,
      page:1,

      note:e.detail,
      value3:e.detail,
    })
    this.getList()
  },
  //条码搜索
  onSearch_code(e){
    var that = this
    this.setData({
      showSelList:!that.data.showSelList,
      page:1,
      code:e.detail,
      value4:e.detail,
    })
    this.getList()
  },
  //供应商搜索
  onSearch_cust_no(e){
    var that = this
    this.setData({
      showSelList:!that.data.showSelList,
      page:1,

      cust_no:e.detail,
      value5:e.detail,
    })
    this.getList()
  },
  //货品搜索
  onSearch_product_name(e){
    var that = this
    this.setData({
      showSelList:!that.data.showSelList,
      page:1,

      value6:e.detail,
      product_name:e.detail
    })
    this.getList()
  },

  onCollapseChange(event) {
    this.setData({
      activeNames: event.detail
    });
  },
  
})